﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;

namespace WebApplication1.ConfigManagement
{
    public partial class ConfigManagement : System.Web.UI.Page
    {
        private int var_TypeID
        {
            get { return int.Parse(ViewState["TypeID"].ToString()); }
            set { ViewState["TypeID"] = value; }
        }

        private Config c;

        public ConfigManagement()
        {
            c = new Config();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindConfigType();
            }
        }

        public void BindConfigType()
        {
            ConfigType ctype = new ConfigType();
            this.DropDownList1.DataSource = ctype.LoadEntities();
            this.DropDownList1.DataTextField = "TypeName";
            this.DropDownList1.DataValueField = "TypeID";
            this.DropDownList1.DataBind();
            System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem("--请选择--", "0");
            this.DropDownList1.Items.Insert(0, li);
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var_TypeID = int.Parse(this.DropDownList1.SelectedValue);
            BindConfig();
        }

        private void BindConfig()
        {            
            if (var_TypeID == 0)
            {
                this.GridView1.DataSource = null;
                this.GridView1.DataBind();
            }
            else
            {
                this.GridView1.DataSource = c.Config_GetAll_forSingleType((enum_configType)var_TypeID);
                this.GridView1.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (var_TypeID == 0)
            {
                string str = "请选择要管理的信息类型！";
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
            }
            else
            {
                ConfigInfo cinfo = new ConfigInfo();
                cinfo.ItemName = this.TextBox1.Text.Trim();
                cinfo.TypeID = var_TypeID;
                c.Insert(cinfo);
                BindConfig();
            }
        }

        protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[1].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
        {
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
            c.Delete(id);
            BindConfig();
        }
    }
}
